package com.soft.servlet;

import com.soft.entity.OrderRefundRecord;
import com.soft.service.Domain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName TuiKuanServlet
 * @Description 管理员处理退款servlet
 * @Date 2021/5/27 11:32
 * @Version 1.0
 */

@WebServlet(name = "TuiKuanServlet", urlPatterns = "/TuiKuan")
public class TuiKuanServlet extends HttpServlet {

    private Domain domain;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();

        int orderId = Integer.parseInt(req.getParameter("orderId"));
        OrderRefundRecord ord = domain.getOrderRefundRecordByOrderId(orderId);

        if (ord == null) {
            req.getSession().setAttribute("flag", 25);
            req.getRequestDispatcher("/OrderManageList").forward(req, resp);
        }else if (ord.getStatus() == OrderRefundRecord.STATUS.STATUS_CREATED) {
            req.setAttribute("OrderRefundRecord", ord);
            req.getRequestDispatcher("tuikuan.jsp").forward(req, resp);
        } else {
            req.getSession().setAttribute("flag", 20);
            req.getRequestDispatcher("/OrderManageList").forward(req, resp);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();

        int id = Integer.parseInt(req.getParameter("member.id"));
        String sum = req.getParameter("member.sum");
        String detail = req.getParameter("member.detail");

        OrderRefundRecord ord = domain.getOrderRefundRecordById(id);
        ord.setSum(sum);
        ord.setDetail(detail);
        ord.setStatus(String.valueOf(OrderRefundRecord.STATUS.STATUS_SUCCESS));

        if (domain.updateOrderRefundRecord(ord)) {
            req.getSession().setAttribute("flag", 18);
        }
        else
            req.getSession().setAttribute("flag", 19);
        req.setAttribute("Ord", ord);
        req.getRequestDispatcher("tuikuan.jsp").forward(req, resp);
    }
}
